package com.loop.unclelend.pro

import android.view.View
import com.loop.network.OkHttp
import com.loop.network.callback.StringCallback
import com.loop.network.model.Response
import com.loop.unclelend.MyApplication
import com.loop.unclelend.base.BaseActivity
import com.loop.unclelend.network.IHttpCallback
import com.loop.unclelend.utils.AesUtil
import com.loop.unclelend.utils.dialog.Dialog_Kefu
import com.loop.unclelend.utils.dialog.Dialog_Tips
import com.loop.unclelend.utils.event.UncleEventData
import com.loop.unclelend.utils.event.UncleSubscriber
import com.loop.unclelend.utils.log.JsonFormat
import com.loop.unclelend.utils.log.UncleLog
import org.json.JSONObject

/**
 * UncleLend
 * @Author wwdeng
 * @CreateTime: 2024/9/3 11:58
 * @Annotation：
 */
abstract class ProActivity: BaseActivity(), UncleSubscriber,IProView {

    var errorDialog: Dialog_Tips? = null

    override fun getAct(): ProActivity {
        return this
    }

    override fun onStart() {
        super.onStart()
        MyApplication.app.publisher.register(this)
    }

    /**
     * 客服
     */
    fun showUncleKefuDialog(eventRemark: String = ""){
        Dialog_Kefu(this)
            .setUncleCancelable(true)
            .setUncleCanceledOnTouchOutside(true)
            .setUncleKefuRemark(eventRemark)
            .setUncleKefuCallback(object : Dialog_Kefu.IDialogCallback{
                override fun onClick(view: View?) {
                }
            })
            .show()
    }

    /**
     * 错误提示
     */
    fun showErrorTipsDialog(tips: String,eventRemark: String = ""){
        if (errorDialog == null){
            errorDialog = Dialog_Tips(this,eventRemark,object : Dialog_Tips.IDialogCallback{
                override fun onClick(view: View?) {

                }
            })
        }
        if (errorDialog!!.isShowing){
            return
        }
        errorDialog!!.show()
        errorDialog!!.isDoubleButton(false)
        errorDialog!!.setTipsText(tips)
    }

    /**
     * post
     */
    fun doPostAction(httpUrl: String,params: JSONObject,callback: IHttpCallback){
        val aesParams = AesUtil.encryptDefault(params.toString())
        UncleLog.e("HttpUrl：  $httpUrl")
        UncleLog.e("HttpParams： $params")
        UncleLog.e("HttpParams AES：  $aesParams")

        OkHttp.post<String>(httpUrl) // 请求方式和请求url
            .tag(this) // 请求的 tag, 主要用于取消对应的请求
            .upString(aesParams)
            .execute(object : StringCallback() {
                override fun onSuccess(response: Response<String>) {
                    val body = response.body()
                    if (response.code() != 200 || body == null || body.isEmpty()){
                        val errorObj = JSONObject()
                        errorObj.put("errorCode",response.code())
                        errorObj.put("errorMsg",response.message())
                        callback.onRequestError(errorObj)
                        return
                    }

                    val result = AesUtil.decryptDefault(body)
                    if (result.isNotEmpty()){
                        Thread { UncleLog.i(JsonFormat.format(result)) }.start()
                        val data = JSONObject(result)
                        callback.onRequestSuccess(data)
                    } else {
                        val errorObj = JSONObject()
                        errorObj.put("errorCode",-1)
                        errorObj.put("errorMsg","http request error $result")
                        callback.onRequestError(errorObj)
                    }
                }

                override fun onError(response: Response<String>) {
                    val errorObj = JSONObject()
                    errorObj.put("errorCode",response.code())
                    errorObj.put("errorMsg",response.message())
                    callback.onRequestError(errorObj)
                }
                override fun onFinish() {
                    callback.onRequestFinish()
                }
            })
    }

    /**
     * 发送事件通知
     */
    fun publishEventData(eventData: UncleEventData){
        MyApplication.app.publisher.publish(eventData)
    }

    /**
     * 处理订阅事件
     */
    override fun onUncleEvent(event: UncleEventData) {
    }

    override fun onDestroy() {
        super.onDestroy()
        OkHttp.getInstance().cancelTag(this)
        MyApplication.app.publisher.detach(this)
    }
}